package com.example.buyticketlishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baselibrary.Utils.SelfToastUtil;
import com.example.baselibrary.Utils.TimeUtil;
import com.example.buyticketlishi.R;
import com.example.buyticketlishi.bean.RzfindTimeControlListResultBean;

import java.util.List;

public class TimeSelectAdapter extends RecyclerView.Adapter<TimeSelectAdapter.ViewHolder> {

    private Context context;
    private List<RzfindTimeControlListResultBean.DataBean.TimeControlListBean> dataBeanList;
    private int number;
    private int oldPosition;
    private int pos;
    private List<String> itemSelect;
    private ItemSelectListener itemSelectListener;

    public TimeSelectAdapter(Context context, List<RzfindTimeControlListResultBean.DataBean.TimeControlListBean> dataBeanList, int number, int pos,List<String> itemSelect) {
        this.context = context;
        this.dataBeanList = dataBeanList;
        this.number = number;
        this.pos = pos;
        this.itemSelect = itemSelect;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.wigit_time_select_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {

        // 根据所选人数，以及时间进行显示   最晚的时间  小于 当前时间 就不可选
        final boolean isOver = TimeUtil.timeOver(dataBeanList.get(position).getControlEndTime());

        if (isOver) {
            //如果超时，那么就显示灰色，设置不可以点击
            holder.ticketTime.setBackgroundColor(context.getResources().getColor(R.color.color_gray));
            holder.ticketNumber.setBackgroundColor(context.getResources().getColor(R.color.color_gray));
            holder.lltimeSelect.setClickable(false);
        } else {
            if (dataBeanList.get(position).isClick()) {
                holder.ticketTime.setBackgroundColor(context.getResources().getColor(R.color.color_yellow));
                holder.ticketNumber.setBackgroundColor(context.getResources().getColor(R.color.color_yellow));
            } else {
                holder.ticketTime.setBackgroundColor(context.getResources().getColor(R.color.color_number));
                holder.ticketNumber.setBackgroundColor(context.getResources().getColor(R.color.color_number));
            }
            holder.lltimeSelect.setClickable(true);
        }

        //如果时段剩余票数，少于传递过来的人数选着，那么就变成灰色
        if (dataBeanList.get(position).getResidueSum() < number) {
            holder.ticketTime.setBackgroundColor(context.getResources().getColor(R.color.color_gray));
            holder.ticketNumber.setBackgroundColor(context.getResources().getColor(R.color.color_gray));
            holder.lltimeSelect.setClickable(false);
        }

        holder.ticketTime.setText(dataBeanList.get(position).getControlStartTime().substring(0, 5) + "-" + dataBeanList.get(position).getControlEndTime().substring(0, 5));
        holder.ticketNumber.setText("剩余：" + dataBeanList.get(position).getResidueSum());

        holder.lltimeSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOver || dataBeanList.get(position).getResidueSum() < number) {
                    //如果超时就不响应点击事件
                } else {
                    //点击了就去响应事件
//                    SelfToastUtil.getInstance().show("我被点击了" + position);
                    holder.ticketTime.setBackgroundColor(context.getResources().getColor(R.color.color_yellow));
                    holder.ticketNumber.setBackgroundColor(context.getResources().getColor(R.color.color_yellow));
                    dataBeanList.get(oldPosition).setClick(false);
                    dataBeanList.get(position).setClick(true);
//                    dataBeanList.get(oldPosition).setClickPos("-1");
//                    dataBeanList.get(position).setClickPos(pos + "-" + position);
                    itemSelect.set(pos,  position + "");
                    itemSelectListener.itemData(itemSelect);
                    notifyItemChanged(position);
                    notifyItemChanged(oldPosition);
                    oldPosition = position;
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView ticketTime;
        private final TextView ticketNumber;
        private final LinearLayout lltimeSelect;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ticketTime = itemView.findViewById(R.id.tv_time_select_show);
            ticketNumber = itemView.findViewById(R.id.tv_time_select_have_ticket_number);
            lltimeSelect = itemView.findViewById(R.id.ll_time_select);
        }
    }

    public interface ItemSelectListener{
        void itemData(List<String> itemDate);
    }

    public void setItemSelectListener(ItemSelectListener selectListener){
        itemSelectListener = selectListener;
    }

}
